package com.lky.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lky.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lky.mall.product.entity.AttrEntity;
import com.lky.mall.product.entity.CategoryEntity;
import com.lky.mall.product.vo.AttrRespVo;
import com.lky.mall.product.vo.AttrVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.lky.mall.product.service.AttrService;
import com.lky.common.utils.PageUtils;
import com.lky.common.utils.R;



/**
 * 商品属性
 *
 * @author laikeyi
 * @email laikeyi@gmail.com
 * @date 2025-08-18 14:45:25
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    /*
     * page: 1,//当前页码
     * limit: 10,//每页记录数
     * sidx: 'id',//排序字段
     * order: 'asc/desc'.//排序方式
     * key: '华为'//检索关键字
     * */
    /*
     * catelogId是菜单的id,点击三级菜单就返回该菜单所有数据
     * attrType 可以是base查基础属性,也可以是sale查销售属性
     * */
    @RequestMapping("/{attrType}/list/{catelogId}")
    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId,
                  @PathVariable("attrType") String type){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /*
    * 获取属性分组的关联的所有属性
    * */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entityList);
    }

    /*
     * 获取属性分组的非关联的所有属性
     * */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAattr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @Transactional
    @RequestMapping("/update")
    @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    /**
     * 批量删除
     * 发过来的数据：“attrId”:1,"attrGroupId":2
     */
    @RequestMapping("/batch/delete")
    @RequiresPermissions("product:attr:delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntity){
        attrService.deleteRelation(Arrays.asList(attrAttrgroupRelationEntity));
        return R.ok();
    }
}
